package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MapHprMprTable {
  private static final Log _log = LogFactory.getLog(MapHprMprTable.class);
  
  private static final Map<Integer, MapHprMpr> _maphprmprList = new HashMap<>();
  
  private static MapHprMprTable _instance;
  
  private MapHprMprTable() {
    load();
  }
  
  public static MapHprMprTable get() {
    if (_instance == null)
      _instance = new MapHprMprTable(); 
    return _instance;
  }
  
  public void load() {
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement("SELECT * FROM `w_指定地图回血魔`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int mapid = rs.getInt("mapid");
        int hpr = rs.getInt("hpr");
        int mpr = rs.getInt("mpr");
        MapHprMpr map = new MapHprMpr();
        map._hpr = hpr;
        map._mpr = mpr;
        _maphprmprList.put(new Integer(mapid), map);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
    _log.info("载入地图回血回魔:" + _maphprmprList.size());
  }
  
  public int getMapHpr(L1PcInstance pc, int mapid) {
    if (_maphprmprList.containsKey(Integer.valueOf(mapid))) {
      if (pc.getMap().isUnderwater()) {
        if (pc.getInventory().checkEquipped(20207))
          return 0; 
        if (pc.hasSkillEffect(1003))
          return 0; 
        if (pc.getInventory().checkEquipped(21048) && pc.getInventory().checkEquipped(21049) && 
          pc.getInventory().checkEquipped(21050))
          return 0; 
      } 
      return (_maphprmprList.get(Integer.valueOf(mapid)))._hpr;
    } 
    return 0;
  }
  
  public int getMapMpr(int mapid) {
    if (_maphprmprList.containsKey(Integer.valueOf(mapid)))
      return (_maphprmprList.get(Integer.valueOf(mapid)))._mpr; 
    return 0;
  }
  
  private class MapHprMpr {
    private int _hpr;
    
    private int _mpr;
    
    private MapHprMpr() {}
  }
}
